package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.TodayCallnameAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 今日点名界面
 *
 * @author yingchunli
 */
public class TodayCallNameActivity extends BaseSoocActivity {

    @InjectView(R.id.btn)
    Button commitBtn;//确定按钮
    @InjectView(R.id.class_todaycallname)
    UltimateRecyclerView listTodayCallname;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    TodayCallnameAdapter adapter;//适配器
    final static int INTENT_CREATE_CLASS = 1;//跳转到创建课堂界面的常量
    final static int INTENT_SIGNINFO = 2;//跳转到签到详情界面的常量
    int maxLastPositon;//分页最后位置
    CallNameDao dao;
    @InjectView(R.id.blank_view)
    RelativeLayout layout;//空白页布局
    @InjectView(R.id.blank_icon)
    ImageView imageView;//空白页的图片
    @InjectView(R.id.tipBlank)
    TextView textView;//空白页提示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_today);
        dao = new CallNameDao(this);
        ButterKnife.inject(this);
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        listTodayCallname.setLayoutManager(linearLayoutManager);
        //获取数据
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        dao.getTodayClassList(PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TAODAYCALSS_FRIST);
    }

    /**
     * 初始化数据
     */
    private void initView() {
        adapter = new TodayCallnameAdapter(dao.getClassInfoList(), this);
        listTodayCallname.setAdapter(adapter);
        listTodayCallname.enableLoadmore();
        listTodayCallname.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                        PageConst.pageNo = 1;
                        dao.getTodayClassList(PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TAODAYCALSS_PUSHDOWN);//获取数据
                        linearLayoutManager.scrollToPosition(0);
                    }
//
        });
        listTodayCallname.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                        if (itemsCount > (PageConst.pageSize - 1)) {
                            maxLastPositon = maxLastVisiblePosition;
                            dao.getTodayClassList(++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TAODAYCALSS_PULLUP);
                        } else {
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                        }
                    }
        });
        adapter.setmOnItemClickLitener(new TodayCallnameAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, ClassInfo classInfo) {
                //签到开关改变
                if (classInfo.getQdzt().equals("0")) {
                    //打开签到开关
                    dao.controlSign(classInfo.getKtid(), "1", CodeConst.EXPAND_SIGN_CONTROL);
                } else {
                    //关闭签到开关
                    dao.controlSign(classInfo.getKtid(), "0", CodeConst.EXPAND_SIGN_CONTROL);
                }
            }

            @Override
            public void OnItemClick(ClassInfo classInfo) {
                //跳转到签到详情界面
                Intent intent = new Intent(TodayCallNameActivity.this, SignInfoActivity.class);
                intent.putExtra("ktid", classInfo.getKtid());
                intent.putExtra("kcid", classInfo.getKcid());
                intent.putExtra("pzqd", classInfo.getPzqd() + "");
                startActivityForResult(intent, INTENT_SIGNINFO);
                AnimUtil.intentSlidIn(TodayCallNameActivity.this);
            }
        });
    }

    /**
     * 回填数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK != resultCode)
            return;

        switch (requestCode) {
            case INTENT_SIGNINFO:
                //从签到详情页面返回
                //获取数据
                PageConst.pageNo = 1;
                dao.getTodayClassList(PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TAODAYCALSS_PUSHDOWN);
                break;

            case INTENT_CREATE_CLASS:
                //从创建课堂页面返回
                if (data.getStringExtra("result").equals("1")) {
                    Log.d("111111");
                    //获取数据
                    PageConst.pageNo = 1;
                    dao.getTodayClassList(PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TAODAYCALSS_FRIST);
                } else {
                    Log.d("00000");
                }
                break;
        }
    }

    /**
     * 无数据时，空白页的处理
     */
    void setBlankView() {
        layout.setVisibility(View.VISIBLE);
        imageView.setImageResource(R.mipmap.blank_course);
        textView.setText("今日暂没点名，立即去创建");
        commitBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_GET_TAODAYCALSS_FRIST://首次请求
                        //获取数据成功
                        initView();//初始化界面数据
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            listTodayCallname.hideEmptyView();
                            layout.setVisibility(View.GONE);
                        } else {
                            //无数据处理
                            setBlankView();
                        }
                        break;

                    case CodeConst.EXPAND_GET_TAODAYCALSS_PUSHDOWN://下拉刷新
                        listTodayCallname.setRefreshing(false);
                        adapter.refresh(dao.getClassInfoList());
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            listTodayCallname.hideEmptyView();

                            layout.setVisibility(View.GONE);
                            Log.d("not null");
                        } else {
                            setBlankView(); //无数据处理
                            Log.d("null");
                        }
                        if (dao.getClassInfoList().size() > (PageConst.pageSize - 1)) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;

                    case CodeConst.EXPAND_GET_TAODAYCALSS_PULLUP://上拉加载
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            adapter.loadmore(dao.getClassInfoList());
                            layout.setVisibility(View.GONE);
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 创建课堂按钮的点击
     */
    @OnClick(R.id.btn)
    void onCreateClassClick() {
        Intent intent = new Intent(this, CreateClassActivity.class);
        intent.putExtra("result", "1");
        startActivityForResult(intent, INTENT_CREATE_CLASS);
        AnimUtil.intentSlidIn(this);
    }

    @Override
    public String setupToolBarTitle() {
        return "今日点名";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }
}
